
#include "main.h"

#define   CELLS_AMOUNT	6

char Previous_Cells[CELLS_AMOUNT][8];
char Current_Cells[CELLS_AMOUNT][8];
	

enum
{
	STATE_START = 10,
	STATE_RESET,
	CHECK_LOCATION,
	SEND_REQUEST_FOR_NEIGHBOR_CELLS,
	GET_NEIGHBOR_CELLS,
	LEARN_LOACATION,
	LEARN_LOACATION__CHECK_AND_SAVE,
	PRINT_SAVED_CELLS,
	SITUATION_ANALYSIS,
	SITUATION_1,
	SITUATION_2,
	
	STATE_TIMEOUT,
	STATE_FINISH
};

unsigned char Compare_Location ( void )
{
	unsigned char i, MatchCount, j;
	
	MatchCount = 0;

	for ( i = 0; i < CELLS_AMOUNT; i++ )
	{
		for ( j = 0; j < CELLS_AMOUNT; j++ )
		{
			if ( StrCmp(Previous_Cells[i], Current_Cells[j]) == 0 )
			{
				MatchCount++;
				break;
			}
		}
	} 
	
	return MatchCount;
} 

void Neighbor_Cells_Print ( void )
{
	char Saved_Cells[CELLS_AMOUNT][8];
	unsigned char i;
	
	MemSet((unsigned char*)Saved_Cells, (CELLS_AMOUNT * 8), 0);
	E2_LoadFrom((unsigned char*)Saved_Cells, E2_ADDR_NEIGHBOR_CELLS, (CELLS_AMOUNT * 8));
	putsf("\n\n  Saved Cells:\n", AUX_PORT);
					
	for ( i = 0; i < CELLS_AMOUNT; i++ )
	{
		putsf("\n",AUX_PORT);
		puts(Saved_Cells[i],AUX_PORT);
	}
	putsf("\n\n",AUX_PORT);
}

unsigned char Ext_Command;

unsigned char Neighbor_Cells_Mode;

void Neighbor_Cells_Control ( unsigned char Command )
{
	if ( Command == NEIGHBOR_CELLS_LEARN )
	{
		Ext_Command = LEARN_LOACATION;
		Neighbor_Cells_Mode = LEARN_LOACATION;
	}
	else
		if ( Command == NEIGHBOR_CELLS_PRINT_SAVED )
		{
			Ext_Command = PRINT_SAVED_CELLS;
		}
}


char Neighbor_Cells_Monitor(void)
{
	static unsigned char State = 0, NextState, Delay, i, j;
	unsigned char Result;
	
	if ( InputsEvent == INPUT_6_DOWN )
	{
		Ext_Command = LEARN_LOACATION;
		Neighbor_Cells_Mode = LEARN_LOACATION;
	}

	
	if ( Neighbor_Cells_Mode )
	{
		if ( Time_100ms )
		{
			LED ^= 1;
		}
	}

	if ( Ext_Command )
	{
		Delay = 0;
	}

	if ( Delay )
	{
		if ( Time_1000ms )
		{
			Delay--;
		}
		return Neighbor_Cells_Mode;
	}

	/* 

+CENG: 2,0

+CENG: 0,"0602,48,0,425,02,54,2ce5,00,00"
+CENG: 1,"0596,47,48"
+CENG: 2,"0603,34,??"
+CENG: 3,"0597,33,??"
+CENG: 4,"0601,32,??"
+CENG: 5,"0595,30,??"
+CENG: 6,"0609,24,22"

 */
	
	switch( State )
	{
		case STATE_START:
		case STATE_RESET:
			//State = LEARN_LOACATION;
			i = 0;

			if ( Ext_Command )
			{
				State = Ext_Command;
				Ext_Command = 0;
			}
			else
			{
				State = CHECK_LOCATION;
			}
			
			break;

			/* Print */
		case PRINT_SAVED_CELLS:
			Neighbor_Cells_Print();
			State = STATE_RESET;
			break;	

			/* Check location */
		case CHECK_LOCATION:
			State = SEND_REQUEST_FOR_NEIGHBOR_CELLS;
			NextState = SITUATION_ANALYSIS;
			break;

		case SITUATION_ANALYSIS:
			E2_LoadFrom((unsigned char*)Previous_Cells, E2_ADDR_NEIGHBOR_CELLS, (CELLS_AMOUNT * 8));
			Result = Compare_Location();
			putsf("\n Neighbor_Cells_Monitor.", AUX_PORT);

/* 			if ( Result == CELLS_AMOUNT ) */
/* 			{ */
/* 				putsf("location no changed\n", AUX_PORT); */
/* 			} */

			putsf(" Changed cells: ", AUX_PORT);
			putch( CELLS_AMOUNT - Result + '0', AUX_PORT);

			if ( CELLS_AMOUNT - Result > 3 )
			{
				OUT_1 = 0; 
				OUT_2 = 1; 
			}
			else
			{
				OUT_1 = 1; 
				OUT_2 = 0; 
			}

			Delay = 5;
			State = STATE_RESET;
			break;
			/* End of check location */


			/* Learn Mode */
		case LEARN_LOACATION:
			putsf("\n\n == Neighbor Cells. Learn location. ==\n", AUX_PORT);
			State = SEND_REQUEST_FOR_NEIGHBOR_CELLS;
			NextState = LEARN_LOACATION__CHECK_AND_SAVE;
			j = 0;
			break;	

		case LEARN_LOACATION__CHECK_AND_SAVE:
			if ( j == 1 )
			{
				if ( Compare_Location() == CELLS_AMOUNT )
				{
					E2_SaveTo(E2_ADDR_NEIGHBOR_CELLS, (unsigned char*)Previous_Cells, (CELLS_AMOUNT * 8));
					Neighbor_Cells_Print();
					putsf("\nLocation was saved\n == Exit from Learn location. ==\n", AUX_PORT);
					
					State = STATE_RESET;

					Neighbor_Cells_Mode = 0;
					Delay = 2;
				}
				else
				{
					putsf("...", AUX_PORT);
					State = SEND_REQUEST_FOR_NEIGHBOR_CELLS;
					NextState = LEARN_LOACATION__CHECK_AND_SAVE;
					Delay = 5;
					j = 0;
				}
			}
			else
				if ( j == 0 )
				{
					MemCpy((unsigned char*)Previous_Cells, (unsigned char*)Current_Cells, (CELLS_AMOUNT * 8));
					
					State = SEND_REQUEST_FOR_NEIGHBOR_CELLS;
					NextState = LEARN_LOACATION__CHECK_AND_SAVE;

					j++;

					Delay = 2;
				}

/* 				else */
/* 				{ */
/* 					State = SEND_REQUEST_FOR_NEIGHBOR_CELLS; */
/* 					NextState = LEARN_LOACATION__CHECK_AND_SAVE; */
/* 					j++; */
/* 				} */
			break;
			/* End of Learn location */
			


		case SEND_REQUEST_FOR_NEIGHBOR_CELLS:
			if ( ModemStatus == READY )
			{
				ModemStatus = BUSY_NEIGHBOR_CELL_MONITOR;
				putsf("AT+CENG?\r", MODEM_PORT);
				State = GET_NEIGHBOR_CELLS; 
			}
			break;   
   
		case GET_NEIGHBOR_CELLS:
			if ( ModemEvents == MODEM_NEW_MESSAGE_PART )
			{
				if ( i < CELLS_AMOUNT && StrCmp("+CENG:", ModemRecBuffer) && errno == 6 && StrLen(ModemRecBuffer) > 10 )
				{	
					StrCutBetweenChars( Current_Cells[i], ModemRecBuffer, ',' );
					i++;
				}
			}
			else
				if ( ModemEvents == MODEM_NEW_MESSAGE_COMPLETE )
				{
					for ( i = 0; i < CELLS_AMOUNT; i++ )
					{
						putsf("\r",AUX_PORT);
						puts(Current_Cells[i],AUX_PORT);
					}

					ModemStatus = READY;
					
					State = NextState; 
				}
			break;


			

/* 		case 	SITUATION_1: */

/* 			break; */

/* 		case 	SITUATION_2: */

/* 			break; */



/* 		case STATE_TIMEOUT: */

/* 			break;  */
      

/* 		case STATE_FINISH: */

/* 			break; */
       

		default:
			State = STATE_START;
	}

	return Neighbor_Cells_Mode;
}
